package handel

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-user/cache"
	"order-user/model"
	"order-user/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	redis_key := "user_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误大于三次禁止登录",
		})
		return
	}
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次封禁两个小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func RoleNode(c *gin.Context) {
	role_id, _ := strconv.Atoi(c.PostForm("role_id"))
	node_id, _ := strconv.Atoi(c.PostForm("node_id"))
	data := model.RoleNode{
		RoleId: role_id,
		NodeId: node_id,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func UserRoleList(c *gin.Context) {
	/*
			SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.role_id = roles.id
		WHERE
		  user_roles.user_id = 1
	*/
	id := c.Query("id")
	var role []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.role_id = roles.id").
		Where("user_roles.user_id = ?", id).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": role,
	})
}
func RoleNodeList(c *gin.Context) {
	/*
			SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.node_id = nodes.id
		WHERE
		  role_nodes.role_id=3
	*/
	var node model.Node
	id := c.Query("id")
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.node_id = nodes.id").
		Where("role_nodes.role_id=?", id).
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": node,
	})
}
func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	data := model.Goods{
		GoodsCode: good_code,
		GoodsName: good_name,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	good_id, _ := strconv.Atoi(c.PostForm("good_id"))
	data := model.Order{
		OrderCode:  order_code,
		OrderPrice: order_price,
		GoodsId:    good_id,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	/*
			SELECT
		  goods.goods_code as GoodsCode,
		  goods.goods_name as GoodsName,
		  orders.order_code as OrderCode,
		  orders.order_price as OrderPrice
		FROM
		  orders
		  LEFT JOIN goods ON orders.goods_id = goods.id
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var list []model.List
	redis_key := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"goods.goods_code as GoodsCode",
				"goods.goods_name as GoodsName",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN goods ON orders.goods_id = goods.id").
			Offset(offset).Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"total": total,
		"data":  list,
	})
}
func OrderFind(c *gin.Context) {
	id := c.Query("id")
	var list model.List
	redis_key := "order_find_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"goods.goods_code as GoodsCode",
				"goods.goods_name as GoodsName",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN goods ON orders.goods_id = goods.id").
			Where("orders.goods_id=?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": list,
	})
}
